import menulist from '@/assets/list.json'

const GenerateRoutes = (routes, roles) => {
  const res = []
  routes.forEach(route => {
    const tmp = { ...route }

    if (hasPermission(roles, tmp)) {
      if (tmp.children) {
        tmp.children = GenerateRoutes(tmp.children, roles)
      }
      res.push(tmp)
    }
  })

  return res
}

function hasPermission (roles, route) {
  if (route.meta && route.meta.roles) {
    return roles.some(role => route.meta.roles.includes(role))
  } else {
    return true
  }
}

function getrow () {
  const userstr = window.sessionStorage.getItem('user')
  const user = JSON.parse(userstr)
  let roles = []

  if (user.permissions == 2) {
    // 管理员
    roles = ['ROLE_ADMIN']
  } else {
    // 普通学生
    roles = ['ROLE_USER']
  }
  // console.log(roles, menulist.menulist)
  return GenerateRoutes(menulist.menulist, roles)
}
// console.log(roles,menulist.menulist)
export default getrow
